﻿
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

using MicroCenter.Models;
using MicroCenter.Services;
using MicroCenter.Tokens;

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;

namespace MicroCenter
{
    public class Startup
    {
        private readonly IConfiguration configuration;

        public Startup(IConfiguration configuration)
        {
            this.configuration = configuration;
        }

        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddAuth<AdminToken>();
            services.ConfigureWritable<MicroCenterOptions>(configuration.GetSection(ConfigConst.MICROCENTER_KEY));
            services.ConfigureWritable<NginxOptions>(configuration.GetSection(ConfigConst.NGINX_KEY));
            services.AddJob();
            services.AddAutoGC(TimeSpan.FromMinutes(30));
            services.AddInjection();
            services.AddApiConfig();
            services.AddSignalRConfig();
            // services.AddSwaggerExt();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        { 
            //if (env.IsDevelopment())
            //{
            //    app.UseSwagger();
            //    app.UseSwaggerUI(options =>
            //    {
            //        options.SwaggerEndpoint("/swagger/v1/swagger.json", "MicroCenterAPI V1");
            //        options.RoutePrefix = "";
            //    });
            //}
            //else
            //{
            //    app.UseSwagger();
            //    app.UseSwaggerUI(options =>
            //    {
            //        options.SwaggerEndpoint("/swagger/v1/swagger.json", "MicroCenterAPI V1");
            //        options.RoutePrefix = "";
            //    });
            //}
            app.UseApiConfig();
            app.UseJob();
            string staticPath = Path.Combine(env.WebRootPath, "static");
            var def = new DefaultFilesOptions()
            {
                FileProvider = new PhysicalFileProvider(staticPath),
            };
            def.DefaultFileNames.Clear();
            def.DefaultFileNames.Add("index.html");
            app.UseDefaultFiles(def);
            app.UseStaticFiles(new StaticFileOptions()
            {
                FileProvider = new PhysicalFileProvider(staticPath),
            });
        }


    }

}
